package com.dmx.platform.utils;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *@author:Beni Yang
 *@date:2012-8-22
 *@function:
 */
public class FileProcess {
	
	private static Log log=LogFactory.getLog(FileProcess.class);
	
	public static void main(String[] str){
		System.out.print(File.separatorChar);
		System.out.print(System.getProperty(File.pathSeparator));
	}
	
	public static String createPath(String sotrePath,Integer id) {
//		String path = this.getImageStorePath();	
		sotrePath += File.separatorChar + String.valueOf(id);
		try {
			File file = new File(sotrePath);
			if (!file.isDirectory()) {
				if (!file.mkdirs()) {
					log.warn("Cann't create directory with " + sotrePath);
					return null;
				}
			}
		} catch (Exception e) {
			log.error("Cann't create directory with " + sotrePath + ",because of " + e.getMessage());
			return null;
		}
		
		log.info("Image path is " + sotrePath);
		return sotrePath;
	}
	
	
	public static String uploadFile(String targetDirectory,String targetFileName,File upload){
		String fileName=targetDirectory+File.separatorChar+targetFileName;
		try {
            File target = new File(targetDirectory, targetFileName);
            FileUtils.copyFile(upload, target);            
            
        } catch (Exception e) {
        	log.error("Upload file error "+e.getMessage());
        	return null;
        }
        return fileName;
	}
	
	public static boolean deleteFile(String pathFileName){
		File file=new File(pathFileName);		
		return file.delete();
	}
	

}
